package com.duoduo.demo.springnoxml.websocket;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.websocket.Session;

/**
 * 控制台日志处理线程
 * @url http://blog.csdn.net/xiao__gui/article/details/50041673
 * @author chengesheng@kedacom.com
 * @date 2017年12月7日上午10:57:14
 */
public class ConsoleWebsocketThread extends Thread {

	private BufferedReader reader;
	private Session session;

	public ConsoleWebsocketThread(InputStream in, Session session) {
		this.reader = new BufferedReader(new InputStreamReader(in));
		this.session = session;

	}

	@Override
	public void run() {
		String line;
		try {
			while ((line = reader.readLine()) != null) {
				// 将实时日志通过WebSocket发送给客户端，给每一行添加一个HTML换行
				session.getBasicRemote().sendText(line + "<br>");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
